package controller

import (
	"encoding/json"
	"log"
	"net/http"
)

type Search struct {
	Id   float64 `json:"id"`
	Name string  `json:"name"`
}

func registerSearchRouters() {
	http.HandleFunc("/search", handleSearch)
}

func handleSearch(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodPost:
		dec := json.NewDecoder(r.Body)
		search := Search{}
		err := dec.Decode(&search)
		if err != nil {
			log.Println(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		enc := json.NewEncoder(w)
		err = enc.Encode(search)
		if err != nil {
			log.Println(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}
